
import java.awt.List;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;



public class Bai_3 {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws NumberFormatException 
	 */
	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		
		Truong t = new Truong();	
		t.ReadListHocSinhMe("hocsinh.txt");
		t.Xuat(1);
		boolean check = true;
		
		while(check)
		{
		 check = t.ChonMenu();
		}
		
				
				
	}

}




////////////////


class HocSinh {

	
	/**
	 * @param args
	 */

	
	private int maSV;
	private String hoTen;
	private String ghiChu;
	private float diem;
	
	public int GetMaSV()
	{
		return this.maSV;
	}
	
	public void SetMaSV(int maSV)
	{
		this.maSV = maSV;
	}
	
	public float GetDiem()
	{
		return this.diem;
	}
	public void SetDiem(float diem)
	{
		this.diem = diem;
	}
	public String GetHoTen()
	{
		return this.hoTen;
	}
	public void SetHoTen(String hoTen)
	{
		this.hoTen = hoTen;
	}
	
	
	public String GetghiChu()
	{
		return this.ghiChu;
	}
	
	public void SetghiChu(String ghiChu)
	{
		this.ghiChu = ghiChu;
	}
	
	// cac ham khoi tao
	
	public HocSinh()
	{
		this.maSV = 0;
		this.hoTen = "no name";
		this.ghiChu = "none";
		this.diem = 0;
	}
	public HocSinh(HocSinh hs)
	{
		this.maSV = hs.maSV;
		this.hoTen = hs.hoTen;
		this.diem = hs.diem;
		this.ghiChu = hs.ghiChu;
	}
	
	public HocSinh(String hoTen, String ghiChu, float diem)
	{
		this.hoTen = hoTen;
		this.maSV = 0;
		this.ghiChu = ghiChu;
		this.diem = diem;
	}
	
	public HocSinh(int maSV , String hoTen , String ghiChu , float diem)
	{
		this.maSV = maSV;
		this.hoTen = hoTen;
		this.ghiChu = ghiChu;
		this.diem = diem;
		
	}
	
	
	public void Nhap() throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Nhap thong tin sinh vien: ");
		int maSV =0;
		int check = 1;
		do
		{
			System.out.print("MaSV: ");	
			check = 1;
			try
			{
				 maSV = Integer.parseInt(br.readLine());
				 
				
			}
			catch(Exception ex)
			{
				System.out.println("MaSV khong hop le vui long nhap lai: ");	
				check = 0;
			}
		}
		while(check == 0 );
		
		float diem=0;
		
		System.out.print("Ho ten: ");
		String hoTen ="";
		do
		{
			 check =1;
			 hoTen = br.readLine();
			 hoTen = hoTen.trim();
			 if(hoTen.length() == 0 )
			 {
				 System.out.println("Ho ten khong duoc de trong.");
				 check =0;
			 }
		}
		while(check == 0);		
		
		do
		{
			System.out.print("Diem: ");		
		
			check = 1;
			try
			{
				 diem = Float.parseFloat(br.readLine());
			}
			catch(Exception ex)
			{
				System.out.println("Diem phai la so >=0 va <= 10");
				check = 0;
			}
		}
		while(check == 0);		
		
		System.out.print("Ghi chu: ");
		String ghiChu = br.readLine();
		
		this.maSV = maSV;
		this.hoTen = hoTen;
		this.ghiChu = ghiChu;
		this.diem = diem;
		
	}
	
	public void Xuat(int option)
	{
		if(option == 0)
		{
			System.out.println("Thong tin sinh vien: ");
			System.out.println("MaSV: " + this.maSV);
			System.out.println("Ho Ten: " + this.hoTen);
			System.out.println("Diem: " + this.diem);
			System.out.println("Ghi chu: " + this.ghiChu);
		}
		else
		{
			System.out.print(this.maSV+"\t\t"+this.hoTen+"\t"+this.diem+"\t\t"+this.ghiChu);
		}
		
	}
	
	public String TaoChuoiHocSinh( )
	{
		String str = "";
		str += this.maSV + "-";
		str += this.hoTen + "-";
		str += this.diem + "-";
		str += this.ghiChu;
		return str;
	}
	public void TaoHocSinh(String str)
	{
		
		String[] temp;
		 
		temp = str.split("-");
		
		this.SetMaSV(Integer.parseInt(temp[0]));
		this.SetHoTen(temp[1]);
		this.SetDiem(Float.parseFloat(temp[2]));
		this.SetghiChu(temp[3]);
					
	
	}
	
	public ArrayList<HocSinh> NhapDanhSachHocSinh() throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		ArrayList<HocSinh> lstHocSinh = new ArrayList<HocSinh>();
		System.out.println("Nhap vao so hoc sinh: ");
		int n = Integer.parseInt(br.readLine());
		for(int i = 0 ; i < n ; i++)
		{
			System.out.println("Hoc sinh thu: "+ (i+1));
			HocSinh hs = new HocSinh();
			hs.Nhap();
			lstHocSinh.add(hs);
			
		}
		return lstHocSinh;
	}
	
	public void XuatDanhSachHocSinh( ArrayList<HocSinh> lstHocSinh) throws NumberFormatException, IOException
	{
		
		
		
		System.out.println("Danh sach hoc sinh: ");
		int n = lstHocSinh.size();
		
		for(int i = 0 ; i < n ; i++)
		{
			System.out.println("Hoc sinh thu: "+ (i+1));
			
			lstHocSinh.get(i).Xuat(1);
			
			
		}
		
	}
	
	public ArrayList<HocSinh> SapXepDanhSachHocSinh(ArrayList<HocSinh> lstHocSinh , int  option)
	{
		int n = lstHocSinh.size();
		
		for(int i = 0 ; i < n-1 ; i++)
		{
			for(int j = i+1 ; j < n; j++)
			{
				boolean hoanVi = false;
				if(option == 1) // maHS tang
				{
					if(lstHocSinh.get(i).GetMaSV() > lstHocSinh.get(j).GetMaSV())
					{
						hoanVi = true;
					}
				}
				else if(option == 2) // maHS giam
				{
					if(lstHocSinh.get(i).GetMaSV() < lstHocSinh.get(j).GetMaSV())
					{
						hoanVi = true;
					}
				}
				else if(option == 3)// diem Tang
				{
					if(lstHocSinh.get(i).GetDiem() > lstHocSinh.get(j).GetDiem())
					{
						hoanVi = true;
					}
				}
				else if(option == 4) // diem giam
				{
					if(lstHocSinh.get(i).GetDiem() < lstHocSinh.get(j).GetDiem())
					{
						hoanVi = true;
					}
				}
				
				if(hoanVi == true)
				{
					HocSinh temp = new HocSinh(lstHocSinh.get(i));
					lstHocSinh.set(i, lstHocSinh.get(j));
					lstHocSinh.set(j, temp);
					
					
				}
			}
		}
		
		
		
		return lstHocSinh;
	}
	
	public HocSinh GetHocSinhByMaSV(int maSV ,ArrayList<HocSinh> lstHocSinh)
	{
		int n = lstHocSinh.size();
		for(int i = 0 ; i < n ; i++)
		{
			if(lstHocSinh.get(i).GetMaSV() == maSV)
			{
				return lstHocSinh.get(i);
			}
		}
		return null;
	}
	
	public void CapNhapHocSinh(String hoTen , float diem , String ghiChu)
	{
		this.hoTen = hoTen;
		this.diem = diem;
		this.ghiChu = ghiChu;
	}
	
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		/*ArrayList<HocSinh> lstHocSinh = new ArrayList<HocSinh>();
		ArrayList<HocSinh> lstHocSinhSort = new ArrayList<HocSinh>();
				
		HocSinh st1 = new HocSinh();
		lstHocSinh = st1.NhapDanhSachHocSinh();
		st1.XuatDanhSachHocSinh(lstHocSinh);
		
		System.out.println("Danh sach sau khi sap xep 1: ");
		lstHocSinhSort = st1.SapXepDanhSachHocSinh(lstHocSinh, 1);		
		st1.XuatDanhSachHocSinh(lstHocSinhSort);
		
		System.out.println("Danh sach sau khi sap xep 2: ");
		lstHocSinhSort = st1.SapXepDanhSachHocSinh(lstHocSinh, 2);		
		st1.XuatDanhSachHocSinh(lstHocSinhSort);
		
		System.out.println("Danh sach sau khi sap xep 3: ");
		lstHocSinhSort = st1.SapXepDanhSachHocSinh(lstHocSinh, 3);		
		st1.XuatDanhSachHocSinh(lstHocSinhSort);
		
		System.out.println("Danh sach sau khi sap xep 4: ");
		lstHocSinhSort = st1.SapXepDanhSachHocSinh(lstHocSinh, 4);		
		st1.XuatDanhSachHocSinh(lstHocSinhSort);
		
		*/
	//	//HocSinh hs = new HocSinh();
	//	hs.Nhap();
//		hs.Xuat(1);
		//st1.Nhap();
		//st1.Xuat();
		
		
	}

}




/////////////////////////////

class Truong {

	/**
	 * @param args
	 */
	
	private ArrayList<HocSinh> lstHocSinh; 
	
	public Truong()
	{
		lstHocSinh = new ArrayList<HocSinh>();
	}
	public void Nhap() throws NumberFormatException, IOException
	{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		
		System.out.println("Nhap vao so hoc sinh: ");
		int n = Integer.parseInt(br.readLine());
		for(int i = 0 ; i < n ; i++)
		{
			System.out.println("Hoc sinh thu: "+ (i+1));
			HocSinh hs = new HocSinh();
			hs.Nhap();
			this.lstHocSinh.add(hs);
			
		}
	}
	
	public void Xuat(int option)throws NumberFormatException, IOException
	{
		
		/*
		SapXep(option);
		
		int n = this.lstHocSinh.size();
		System.out.println("Danh sach hoc sinh: " +n+" hoc sinh:");		
		
		for(int i = 0 ; i < n ; i++)
		{
			System.out.println("Hoc sinh thu: "+ (i+1));			
			lstHocSinh.get(i).Xuat();				
		}
		*/
		

		SapXep(option);
		
		int n = this.lstHocSinh.size();
		System.out.println("Danh sach hoc sinh: " +n+" hoc sinh:");		
		System.out.println("STT	MaHS 		Ten HS		Diem		Ghi Chu");
		for(int i = 0 ; i < n ; i++)
		{
			System.out.print((i+1)+"\t");			
			lstHocSinh.get(i).Xuat(2);	
			System.out.println();
		}
		
		
	}
	
	public void SapXep(int option)
	{
		int n = lstHocSinh.size();
		
		for(int i = 0 ; i < n-1 ; i++)
		{
			for(int j = i+1 ; j < n; j++)
			{
				boolean hoanVi = false;
				if(option == 1) // maHS tang
				{
					if(lstHocSinh.get(i).GetMaSV() > lstHocSinh.get(j).GetMaSV())
					{
						hoanVi = true;
					}
				}
				else if(option == 2) // maHS giam
				{
					if(lstHocSinh.get(i).GetMaSV() < lstHocSinh.get(j).GetMaSV())
					{
						hoanVi = true;
					}
				}
				else if(option == 3)// diem Tang
				{
					if(lstHocSinh.get(i).GetDiem() > lstHocSinh.get(j).GetDiem())
					{
						hoanVi = true;
					}
				}
				else if(option == 4) // diem giam
				{
					if(lstHocSinh.get(i).GetDiem() < lstHocSinh.get(j).GetDiem())
					{
						hoanVi = true;
					}
				}
				
				if(hoanVi == true)
				{
					HocSinh temp = new HocSinh(lstHocSinh.get(i));
					lstHocSinh.set(i, lstHocSinh.get(j));
					lstHocSinh.set(j, temp);
					
					
				}
			}
		}
		
	}
	
	public void XoaHocSinh(int maHS)
	{
		int n = this.lstHocSinh.size();		
		int check = 0;
		for(int i = 0 ; i < n ; i++)
		{
					
			if(lstHocSinh.get(i).GetMaSV() == maHS)	
			{
				lstHocSinh.remove(i);
				check = 1;
				break;
			}
			
		}
		if(check == 1)
		{
			System.out.print("Xoa HS "+maHS+" thanh cong.");
		}
		else
		{
			System.out.print("Khong xoa dx hoc sinh "+maHS);
		
		}
		
		
	}
	
	public HocSinh GetHocSinh(int maHS)
	{
		
		int n = this.lstHocSinh.size();		
		int check = 0;
		for(int i = 0 ; i < n ; i++)
		{
					
			if(lstHocSinh.get(i).GetMaSV() == maHS)	
			{
				return lstHocSinh.get(i);
			}
			
		}
		return null;
	}
	
	public void CapNhatHocSinh(int maHS , String hoTen ,float diem, String ghiChu)
	{
		
		int n = this.lstHocSinh.size();				
		for(int i = 0 ; i < n ; i++)
		{
					
			if(lstHocSinh.get(i).GetMaSV() == maHS)	
			{
				if(diem != -1)
				{
					lstHocSinh.get(i).SetDiem(diem);
				}
				if(ghiChu != "")
				{
					lstHocSinh.get(i).SetghiChu(ghiChu);
				}
				if(hoTen != "")
				{
					lstHocSinh.get(i).SetHoTen(hoTen);
				}
				
				
				System.out.println("Thong tin da duoc cap nhat.");
				return;
			}
			
		}
		
	}
	public void GetThongTinXoaHocSinh() throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		/*
		System.out.print("Nhap MAHS xoa: ");
		int maSV = Integer.parseInt(br.readLine());
		
		XoaHocSinh(maSV);
		*/
		boolean check =true;
		int maSV = 0;
		do
		{
			check = true;
			System.out.print("Nhap MAHS Xoa: ");	
			
			try
			{
				 maSV = Integer.parseInt(br.readLine());
				 // kiem tra co masv hay ko
				 int kt = KiemTraTonTaiSinhVien(maSV);
				 
				 if(kt == 0)// ko ton tai massv
				 {
					check = true;
					System.out.println("MaSV khong ton tai. vui long nhap lai masv de xoa.");
				 }
				 else
				 {
					 XoaHocSinh(maSV);
					 System.out.println("Xoa hoc sinh " + maSV + " thanh cong." );
					 check = false;
				 }
				 //
				 
				
			}
			catch(Exception ex)
			{
				System.out.println("MaSV khong hop le hoac vui long nhap lai. ");	
				
			}
		}
		while(check == true );
			
	
		
	}
	
	public int KiemTraTonTaiSinhVien(int maSV)
	{
	
		
		int n = lstHocSinh.size();
		for(int i = 0 ; i < n ; i++)
		{
			if(lstHocSinh.get(i).GetMaSV() == maSV )
			{
				return 1;
			}
		}
		
		return 0;
	}
	public void GetThongTinCapNhat() throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Luu y: Neu khong thay doi gia tri thi khong nhap gi ca: ");
					
		
		int maSV =0;//= Integer.parseInt(br.readLine().trim());
		
		//
		boolean check = true;
		do
		{
			check = true;
			System.out.print("Nhap MAHS Cap nhat: ");	
			
			try
			{
				 maSV = Integer.parseInt(br.readLine());
				 // kiem tra co masv hay ko
				 int kt = KiemTraTonTaiSinhVien(maSV);
				 
				 if(kt == 0)// ko ton tai massv
				 {
					check = true;
					System.out.println("MaSV khong ton tai. vui long nhap lai masv.");
				 }
				 else
				 {
					 check = false;
				 }
				 //
				 
				
			}
			catch(Exception ex)
			{
				System.out.println("MaSV khong hop le hoac vui long nhap lai: ");	
				
			}
		}
		while(check == true );
		
		//
		System.out.print("Ho ten: ");
		String hoTen = br.readLine().trim();
		
		
		
		float diem;
		
		do
		{
			check = true;
			
			System.out.print("Diem: ");		
			String diemtmp =  br.readLine().trim();
		
			
			
			if(diemtmp.length()==0)
			{
				diem = -1;
			}
			else
			{
				try
				{
					diem = Float.parseFloat(diemtmp);
					check = false;
				}
				catch(Exception ex)
				{
					diem = -1;
				}
			}
			
			if(diem == -1)
			{
				System.out.println("Diem nhap ko hop le.");
			}
		}while(check == true);
		
		System.out.print("Ghi chu: ");
		String ghiChu = br.readLine().trim();
		
		CapNhatHocSinh(maSV, hoTen, diem, ghiChu);
		
	}
	
	public void ReadListHocSinhMe(String filename) throws IOException
	{
		lstHocSinh.clear();
		
		BufferedReader br = new BufferedReader(new FileReader(filename));
		
		
		while (true)
		{
			HocSinh hs = new HocSinh();
			String str = br.readLine();
			
			
			if (str==null)
				break;
			
			
			hs.TaoHocSinh(str);
			lstHocSinh.add(hs);
		
		}		
		
		
	}
	public void WriteListHocSinhMe(String filename)
	{
		try
		{
			
			
			File fOut = new File(filename);
		    PrintWriter out = new PrintWriter(fOut);
		    
		
			int n = lstHocSinh.size();
			System.out.println("write to " + filename + ": ");
			
			for(int i = 0 ; i < n ; i++)
			{
				//System.out.print(arr.get(i) + " ");			
				//out.printf("%d ",arr.get(i));
				String str = lstHocSinh.get(i).TaoChuoiHocSinh();
				out.printf("%s",str);
				out.println();			
			}
			out.close();
		}
	
	  	catch (IOException ioe)
	  	{
	  		System.out.println("error write file!");
	  	}
		
	}
	 public void WriteListHocSinh(String filename) {
	        
	        ObjectOutputStream outputStream = null;
	        
	        try {
	            
	            //Construct the LineNumberReader object
	            outputStream = new ObjectOutputStream(new FileOutputStream(filename));
	            
	            int n = lstHocSinh.size();
	            
	           for(int i = 0 ; i < n ; i++)
	           {

		            outputStream.writeObject(lstHocSinh.get(i));
	           }
	            
	            
	           
	            
	            
	            
	        } catch (FileNotFoundException ex) {
	            ex.printStackTrace();
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        } finally {
	            //Close the ObjectOutputStream
	            try {
	                if (outputStream != null) {
	                    outputStream.flush();
	                    outputStream.close();
	                }
	            } catch (IOException ex) {
	                ex.printStackTrace();
	            }
	        }
	    }
	 
	 public boolean ChonMenu() throws NumberFormatException, IOException
	 {
		 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		 
		 System.out.println("Bạn hay chọn chuc nang:\r\n1- Xem danh sach hoc sinh ma hoc sinh tang dan.\r\n2- Xem danh sach hoc sinh ma hoc sinh giam dan.\r\n3- Xem danh sach hoc sinh diem tang dan.\r\n4- Xem danh sach hoc sinh diem giam dan.");
		 System.out.println("5- Thêm hoc sinh.\r\n6- sua hoc sinh.\r\n7 - xoa hoc sinh.\r\n0-Ket thuc.");
		 int chucNang ;
		 try
		 {
			 System.out.print("Chuc nang: ");
			 chucNang = Integer.parseInt(br.readLine());
		 }
		 catch(Exception ex)
		 {
			 chucNang = -1;
		 }
		 
		 if(chucNang == 0)// xem canh sach hoc sinh
		 {
			 // ghi lai list hoc sinh
			 WriteListHocSinhMe("hocsinh.txt");
			 return false;
		 }
		 else if(chucNang >=  1 && chucNang <= 4)
		 {
			 Xuat(chucNang);
		 }
		 else if(chucNang == 5)// them hoc sinh
		 {
			 ThemHocSinh();
			 WriteListHocSinhMe("hocsinh.txt");
		 }
		 else if(chucNang == 6)// sua hoc sinh
		 {
			 GetThongTinCapNhat();
			 WriteListHocSinhMe("hocsinh.txt");
		 }
		 else if(chucNang == 7)
		 {
			 GetThongTinXoaHocSinh();
			 WriteListHocSinhMe("hocsinh.txt");
		 }
		 else
		 {
			 System.out.println("Chuc nang ko phu hop.");
		 }
		 return true;
	 }
	 
	 public void ThemHocSinh() throws NumberFormatException, IOException
	 {
		 
		 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			
		 	HocSinh hs = new HocSinh();
			System.out.println("Nhap thong tin sinh vien: ");
			int maSV =0;
			int check = 1;
			do
			{
				System.out.print("MaSV: ");	
				check = 1;
				try
				{
					 maSV = Integer.parseInt(br.readLine());
					 int kt = KiemTraTonTaiSinhVien(maSV);
					 
					 if(kt == 1)
					 {
						 System.out.println("MaHS da ton tai. vui long nhap lai maHS.");
						 check = 0;
					 }
					
				}
				catch(Exception ex)
				{
					System.out.println("MaSV khong hop le vui long nhap lai: ");	
					check = 0;
				}
			}
			while(check == 0 );
			
			float diem=0;
			
			System.out.print("Ho ten: ");
			String hoTen ="";
			do
			{
				 check =1;
				 hoTen = br.readLine();
				 hoTen = hoTen.trim();
				 if(hoTen.length() == 0 )
				 {
					 System.out.println("Ho ten khong duoc de trong.");
					 check =0;
				 }
			}
			while(check == 0);		
			
			do
			{
				System.out.print("Diem: ");		
			
				check = 1;
				try
				{
					 diem = Float.parseFloat(br.readLine());
				}
				catch(Exception ex)
				{
					System.out.println("Diem phai la so >=0 va <= 10");
					check = 0;
				}
			}
			while(check == 0);		
			
			System.out.print("Ghi chu: ");
			String ghiChu = br.readLine();
			
			hs.SetMaSV(maSV) ; 
			hs.SetHoTen(hoTen); 
			hs.SetghiChu(ghiChu); 
			hs.SetDiem(diem); 
			
			
			
		// HocSinh hs = new HocSinh();
		//hs.Nhap();
		 lstHocSinh.add(hs);
	 }
	 
	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		/*
		Truong t = new Truong();
		
		
		t.ReadListHocSinhMe("hocsinh.txt");
		t.Xuat(1);
		boolean check = true;
		while(check)
		{
		 check = t.ChonMenu();
		}*/
	}
	


}

